var primaryReferral;
var referralEditState;
var delReferralAry;

function initReferral() {
	var editReferral = function() {
		/* Get the position of the current data from the node */
		var aPos = referralTable.fnGetPosition(this);
		/* Get the data array for this row */
		var aData = referralTable.fnGetData(aPos[0]);
		if (aPos[1] != 0) {
			if (aPos[1] == 1) {
				window.location.replace("talentInfo.jsp?talentID=" + aData[5]);
			} else {
				setReferralDialogField(aData[1], aData[2], aData[3], aData[4],
						aData[5]);
				$("#referral").attr("disabled", true);
				$("#submitAddReferral").attr("value", "Update");
				addReferralDialog.dialogObj.dialog("open");
				referralEditState = "modify";
			}
		}
	}

	/* Draw Tables */
	$.getJSON("/hnl/loadReferral.do?talentID=" + tid, function(data) {
		referralTable = updateTableTemplate(data, "#referralTb",
				"/hnl/template/referralTableTemplate.jsp",
				"#referralListTemplate", true, editReferral, "checkboxAll");
		initReferralTB();
		referralTable.fnSetColumnVis(5, false);
	});

	$("#addReferral").click(function(evt) {
		evt.preventDefault();
		$("#referral").attr("disabled", false);
		setReferralDialogField("", "", "", "false", "");
		addReferralDialog.dialogObj.dialog("open");
		referralEditState = "new";
	});

	$("#referral").autocomplete({
		source : "/hnl/ReferralAutocomplete.do",
		minLength : 2,
		select : function(event, ui) {
			$("#referralID").attr("value", ui.item.Id);
		}
	});

	var referralWarningDialog = new HnlDialogObj("#dialog-no-referral-selected");
	referralWarningDialog.buildDialog();
	referralWarningDialog.addDialogFunctions();

	var referralExistWarningDialog = new HnlDialogObj("#dialog-referral-exist");
	referralExistWarningDialog.buildDialog();
	referralExistWarningDialog.addDialogFunctions();
	
	var referralInvalidInputWarningDialog = new HnlDialogObj("#dialog-referral-fields-required");
	referralInvalidInputWarningDialog.buildDialog();
	referralInvalidInputWarningDialog.addDialogFunctions();

	var deleteReferralDialog = new HnlDialogObj("#dialog-deleteReferral");
	deleteReferralDialog.buildDialog();
	deleteReferralDialog.addDialogFunctions(function() {
		$(this).dialog("close");
		$.ajax({
			type : "POST",
			url : "/hnl/DeleteReferral.do",
			data : {
				talentID : tid,
				deleteList : delReferralAry.join(",")
			},
			dataType : "json",
			success : function(data) {
				referralTable = updateTableTemplate(data, "#referralTb",
						"/hnl/template/referralTableTemplate.jsp",
						"#referralListTemplate", true, editReferral,
						"checkboxAll");
				initReferralTB();
				referralTable.fnSetColumnVis(5, false);
			}
		});
	});

	$("#delReferral").click(
			function(evt) {
				evt.preventDefault();
				delReferralAry = [];
				delReferralAry = getCheckedArray("#referralListTemplate",
						"tableCheckbox");
				if (delReferralAry.length == 0) {
					referralWarningDialog.dialogObj.dialog("open");
					return false;
				} else {
					deleteReferralDialog.dialogObj.dialog("open");
				}
			});

	var addReferralDialog = new HnlDialogObj("#dialog-referral");
	addReferralDialog.height = 450;
	addReferralDialog.width = 600;
	addReferralDialog.buildDialog();
	addReferralDialog.addDialogFunctions(function() {
		if (hasInvalidInput_referral()) {
			// no action
			referralInvalidInputWarningDialog.dialogObj.dialog("open");		
			return false;
		}
		var tmp = [];
		var referralExist = false;
		addReferralDialog.dialogObj.dialog("close");
		$("#referralListTemplate input:checkbox[name='tableCheckbox']").each(
				function() {
					if (referralEditState == "new") {
						tmp = $(this).val().split("_");
						if (tmp[0] == $("#referralID").val()) {
							referralExist = true;
						}
					}
				});
		if (referralExist) {
			referralExistWarningDialog.dialogObj.dialog("open");
			return false;
		}
		$.ajax({
			type : "POST",
			url : "/hnl/AddReferralInfo.do?referralEditstate="
					+ referralEditState,
			data : {
				talentID : tid,
				referralID : $("#referralID").val(),
				isPrimary : $("#isPrimary").is(":checked"),
				referralComment : $("#referralComment").val(),
				relationship : $("#relationship").val(),
				oldPrimary : primaryReferral
			},
			dataType : "json",
			success : function(data) {
				referralTable = updateTableTemplate(data, "#referralTb",
						"/hnl/template/referralTableTemplate.jsp",
						"#referralListTemplate", true, editReferral,
						"checkboxAll");
				initReferralTB();
				referralTable.fnSetColumnVis(5, false);
			}
		});
	})

}

function hasInvalidInput_referral() {
	var referralname = $.trim($("#referral").val());
	var referralrelationship = $.trim($("#relationship").val());
	if (referralname == "" || referralrelationship == "") {
		/*
		 * $("#personalDialogMsg") .attr( "innerHTML", "Required fields missing.</br></br>" + "<font
		 * color='red'>Referral ID and relationship are required.</font></br>");
		 * referralWarningDialog.dialogObj.dialog("open");
		 */
		return true;
	}
	return false;
}

function initReferralTB() {
	$("#referralTb td[name='primary']").each(function() {
		if ($(this).attr("innerHTML") == "true") {
			$(this).attr("innerHTML", "");
			$(document.createElement("img")).attr({
				"src" : "/hnl/images/checked.png"
			}).appendTo($(this));
			primaryReferral = $(this).next().attr("textContent");
		} else {
			$(this).attr("innerHTML", "");
		}
	})
}

function setReferralDialogField(data1, data2, data3, data4, data5) {
	$("#referral").attr("value", data1);
	$("#relationship").attr("value", data2);
	$("#referralComment").attr("value", data3);
	if(data4=="false"){
		$("#isPrimary").attr("checked", false);
	}else{
		$("#isPrimary").attr("checked", true);
	}
	
	$("#referralID").attr("value", data5);
}